package com.gamesnet.ihm;

import java.util.List;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.gamesnet.api.Api;
import com.gamesnet.models.Console;
import com.gamesnet.models.Listable;
import com.gamesnet.util.AdapterListener;
import com.gamesnet.util.MessageDialog;
import com.gamesnet.widget.ListAdapter;

public class ConsoleActivity extends Activity implements AdapterListener{

	long userId;
	@Override
	public void onCreate(Bundle savedInstanceState) { 

		super.onCreate(savedInstanceState); 
		setContentView(R.layout.consoles);
		
		Bundle b = getIntent().getExtras();
		userId = b.getLong("userId");
		
		String id = String.valueOf(userId);
		
		try {
			List<Console> consoles = Api.consoles(id);
			ListAdapter<Console> adapter = new ListAdapter<Console>(this, consoles);
			adapter.addListener(this);
			adapter.setUserId(userId);
			ListView list = (ListView)findViewById(R.id.listConsoles);
			list.setAdapter(adapter);
			
			
		} catch (JSONException ex) {
			ex.printStackTrace();
			Log.e("API CONSOLES", ex.getMessage() );
		}

	}

	public void onClickText(Listable model, int position) {
		Intent intent = new Intent(this, InfoConsoleActivity.class);
		intent.putExtra("consoleId", model.getId());
		startActivity(intent);
	}

	public void onClickSupp(Listable model, int position) {
		MessageDialog.showMessage("Supprimer : "+model.toString(), this);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, 1, 0, "Voir son mur");
		return true; 
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) { 
		case 1:
			Log.v("compte", "clique le menu item");
			Intent intent = new Intent(this, WallActivity.class);
			intent.putExtra("userId", userId);
			startActivity(intent);
			return true;
		default:
			return true;
		} 
	}

}
